package com.huawei.m2m.cig.decoder.protocol;

import java.nio.BufferOverflowException;

public abstract class InputBuffer {
	protected byte[] buf = null;
	protected int pos = 0;
	protected int limit = 0;

	public InputBuffer(byte[] paramArrayOfByte) {
		this(paramArrayOfByte, 0, paramArrayOfByte.length);
	}

	public InputBuffer(byte[] paramArrayOfByte, int paramInt1, int paramInt2) {
		this.buf = paramArrayOfByte;
		this.pos = paramInt1;
		this.limit = (paramInt1 + paramInt2);
	}

	protected void checkLength(int paramInt) {
		if (this.pos + paramInt > this.limit) {
			throw new BufferOverflowException();
		}
	}

	public int getByteSigned() {
		checkLength(1);
		int i = this.buf[(this.pos++)];
		return i;
	}

	public int getByte() {
		return getByteSigned() & 0xFF;
	}

	public abstract int getInt16();

	public abstract int getInt16Signed();

	public abstract int getInt24();

	public abstract int getInt24Signed();

	public abstract int getInt32();

	public abstract int getInt32Signed();

	public int getRemainingLength() {
		return this.limit - this.pos;
	}

	public byte[] getByteArray(int paramInt) {
		checkLength(paramInt);
		byte[] arrayOfByte = new byte[paramInt];
		System.arraycopy(this.buf, this.pos, arrayOfByte, 0, paramInt);
		this.pos += paramInt;
		return arrayOfByte;
	}

	public String getString(int paramInt, String paramString) {
		checkLength(paramInt);
		byte[] arrayOfByte = new byte[paramInt];
		System.arraycopy(this.buf, this.pos, arrayOfByte, 0, paramInt);
		this.pos += paramInt;

		return byteArrayToHexStr(arrayOfByte);
	}

	private String byteArrayToHexStr(byte[] byteArray) {
		if (byteArray == null) {
			return null;
		}
		char[] hexArray = "0123456789ABCDEF".toCharArray();
		char[] hexChars = new char[byteArray.length * 2];
		for (int j = 0; j < byteArray.length; j++) {
			int v = byteArray[j] & 0xFF;
			hexChars[j * 2] = hexArray[v >>> 4];
			hexChars[j * 2 + 1] = hexArray[v & 0x0F];
		}
		return new String(hexChars);
	}
}
